package com.web.controller.crawler;

import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.SerializationUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;
import com.web.model.crawler.TbGrading;
import com.web.rabbitmq.crawler.Rabbit;
import com.web.util.crawler.GetHtml;
import com.web.util.crawler.Json;

/**
 * Grading控制器 商品分类
 * 
 * @author G8670
 * 
 */
@RestController
@RequestMapping("/gradingAction")
public class GradingAction {

	private String QUEUE_NAME = "grading";// 三级分类队列频道标识

	@RequestMapping(value = "/add")
	public Json add() {
		Json j = new Json();

		Connection connection = Rabbit.getConnection();// 获取队列连接
		Channel channel = Rabbit.getChannel(connection, QUEUE_NAME);// 获取队列频道

		TbGrading tb = null;// 一级菜单对象
		TbGrading tb_2 = null;// 二级菜单对象
		TbGrading tb_3 = null;// 三级菜单对象

		String html = GetHtml.get("https://www.taobao.com/markets/tbhome/market-list");
		Document doc = Jsoup.parse(html);
		List<Element> list = doc.getElementsByClass("module-wrap");// 按照一级分类划分模块

		try {
			for (Element element : list) {
				List<Element> J_category_hash = element.getElementsByClass("J_category_hash");// 得到一级分类信息
				for (Element ele : J_category_hash) {
					// 一级菜单赋值
					tb = new TbGrading();
					tb.setId(UUID.randomUUID().toString());
					tb.setGrade("一级菜单");
					tb.setName(ele.text());
					// 存进队列
					channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, SerializationUtils.serialize(tb));

					// System.out.println(ele.text());
					List<Element> category_list = element.getElementsByClass("category-list-item");// 按照二级分类划分模块
					for (Element ele2 : category_list) {
						List<Element> category_name_list = ele2.getElementsByClass("category-name");// 得到二级模块中二三级的信息
						for (int i = 0; i < category_name_list.size(); i++) {
							if (i == 0) {
								// 二级菜单赋值
								tb_2 = new TbGrading();
								tb_2.setId(UUID.randomUUID().toString());
								tb_2.setGrade("二级菜单");
								tb_2.setName(category_name_list.get(i).text());
								tb_2.setTbGrading(tb);
								// 存进队列
								channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, SerializationUtils.serialize(tb_2));

								// System.out.println("二级：" + category_name_list.get(i).text() + "--父级：" + ele.text());
							} else {
								if (category_name_list.get(i).text().length() > 0) {
									// 三级菜单赋值
									tb_3 = new TbGrading();
									tb_3.setId(UUID.randomUUID().toString());
									tb_3.setGrade("三级菜单");
									tb_3.setName(category_name_list.get(i).text());
									tb_3.setUrl(category_name_list.get(i).attr("href"));
									tb_3.setTbGrading(tb_2);
									tb_3.setState(0);
									// 存进队列
									channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, SerializationUtils.serialize(tb_3));
									// System.out.println("三级：" + category_name_list.get(i).text() +category_name_list.get(i).attr("href")+ "--父级：" + category_name_list.get(0).text());
								}
							}
						}

					}
				}

			}

			TbGrading tb_end = new TbGrading("-1");
			// 结束标记存进队列
			channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, SerializationUtils.serialize(tb_end));
			// 关闭频道和连接
			Rabbit.close(connection, channel);

			j.setSuccess(true);
			j.setMsg("添加成功");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("添加失败");
		}
		return j;
	}

}
